<?php

/**
 * @author Meta Sanjaya
 */
class Obj_Module {

    /**
     *
     * @var int
     */
    var $id;
    /**
     *
     * @var name
     */
    var $name;
    /**
     *
     * @var string
     */
    var $uri;
    /**
     *
     * @var Obj_Module
     */
    var $child = array();
}

class Obj_Group {

    /**
     *
     * @staticvar int
     */
    static $admin = 1;
    /**
     *
     * @var int
     */
    var $id;
    /**
     *
     * @var string
     */
    var $name;
    /**
     *
     * @var Obj_Group
     */
    var $child = array();
}

class Obj_Acl {

    /**
     *
     * @var Obj_Group
     */
    var $group;
    /**
     *
     * @var Obj_Module
     */
    var $module = array();
}

class Obj_User {

    /**
     *
     * @var int
     */
    var $id;
    /**
     *
     * @var string
     */
    var $username;
    /**
     *
     * @var string
     */
    var $password;
    /**
     *
     * @var Obj_Group
     */
    var $group;
    /**
     *
     * @var boolean
     */
    var $aktif;
    /**
     *
     * @var int
     */
    var $last_login;
}

class MY_Controller extends CI_Controller {

    /**
     *
     * @var String
     */
    protected $breadcumb = "";
    /**
     *
     * @var CI_Template
     */
    var $template;
    /**
     *
     * @var CI_Session
     */
    var $session;
    /**
     *
     * @var CI_Input
     */
    var $input;
    /**
     *
     * @var CI_Loader
     */
    var $load;
    /**
     *
     * @var CI_Router
     */
    var $router;
    /**
     *
     * @var Obj_User
     */
    var $user;
    /**
     *
     * @var Obj_Acl
     */
    var $acl;
    /**
     *
     * @var Model_acl
     */
    var $model_acl;

    public function __construct() {
        parent::__construct();

        if ($this->router->class != 'login' && !$this->session->userdata('user')) {
            redirect('login');
        }

        if ($this->session->userdata('user')) {
            $this->load->model('model_acl');

            $this->user = json_decode($this->session->userdata('user'));
            $this->acl = $this->model_acl->get_acl($this->user->group);

            $this->template->assignByRef('user', $this->user);
            $this->template->assignByRef('acl', $this->acl);
            $this->template->assignByRef('menu', $this->build_menu($this->acl->module, 'sf-menu', true));
        }

        $this->template->assignByRef('breadcumb', $this->breadcumb);
    }

    /**
     *
     * @param Obj_Module $module
     * @return string
     */
    function build_menu($module=array(), $class='', $root=false) {
        if(!count($module)) return '';
        
        $html = '<ul class="' . $class . '">';

        foreach ($module as $m) {
            $html .= '<li>';
            $html .= '<a id="' . $m->id . '" href="' . (is_null($m->uri) ? 'javascript:void(0)' : site_url($m->uri)) . '">' . $m->name . '</a>';
            $html .= $this->build_menu($m->child);
            $html .= '</li>';
        }

        if($root) {
            $html .= '<li><a href="'.site_url('login/logout').'">Logout</a></li>';
        }
        
        $html .= '</ul>';

        if($root) {
            $html .= '<div class="clear"></div>';
        }

        return $html;
    }

    function debug($var) {
        echo "<pre>";
        var_dump($var);
        echo "</pre>";
        exit;
    }

}

?>